package com.onemile.app.base;

import com.onemile.app.R;
import com.onemile.app.UICart;
import com.onemile.app.UICouponList;
import com.onemile.app.UIFavoriteList;
import com.onemile.app.UILogin;
import com.onemile.app.UIOrderList;
import com.onemile.app.UIbaidu;
import com.onemile.entity.PubConst;
import com.onemile.entity.Session;
import com.onemile.utils.AsyncTaskManager;
import com.onemile.utils.ISupportAsyncTask;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public abstract class BaseUINoTop extends Activity implements ISupportAsyncTask
{
   private AsyncTaskManager _taskManager;

   public AsyncTaskManager getTaskManager()
   {
	  if (_taskManager == null)
		 _taskManager = new AsyncTaskManager(this);
	  return _taskManager;
   }

   private LayoutInflater mInflater; // 实例化layout使用的类
   protected LinearLayout mainLayout; // 主题布局
   protected View contentView; // 页面中间UI部分

   protected View bottomNavigationView;
   private ImageView imgOrder;
   private ImageView imgFavorites;
   private ImageView imgCoupon;
   private ImageView imgShare;
   private ImageView imgMore;

   private int currentIndex=0;
   
   private void InitBaseControls()
   {

	  // 实例化顶部工具栏布局，设置给完整页面Layout的顶部部分。。
	  this.mInflater = LayoutInflater.from(this);
	  
	  bottomNavigationView = mInflater.inflate(R.layout.navigation, null);
	  imgOrder = (ImageView) bottomNavigationView.findViewById(R.id.imgOrder_navigation);
	  imgOrder.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			// TODO我的帐户
			if(currentIndex==1)
			{
			   return;
			}
			if (Session.getInstance().IsLogin())
			{
			    Intent intent = new Intent();
			    intent.setClass(BaseUINoTop.this, UIOrderList.class);
			    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			    startActivity(intent);
			}
			else
			{
//			   SetNavigationStatus(1);
			   Intent intent = new Intent();
			   intent.setClass(BaseUINoTop.this, UILogin.class);
			   intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			   intent.putExtra(PubConst.Intent_Key.LOGIN_FROM_ACTIVITY,PubConst.ActivityCode.ORDER_LIST);
			   startActivity(intent);
			}
		 }
	  });
	  imgFavorites = (ImageView) bottomNavigationView.findViewById(R.id.imgFavorites_navigation);
	  imgFavorites.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			// TODO返回首页
			if(currentIndex==2)
			{
			   return;
			}
			if (Session.getInstance().IsLogin())
			{
			   Intent intent = new Intent();
			   intent.setClass(BaseUINoTop.this, UIFavoriteList.class);
			   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			   startActivity(intent);
			}
			else//去登录
			{
			   Intent intent = new Intent();
			   intent.setClass(BaseUINoTop.this, UILogin.class);
			   intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			   intent.putExtra(PubConst.Intent_Key.LOGIN_FROM_ACTIVITY,PubConst.ActivityCode.FAVORITE_LIST);
			   startActivity(intent);
			}

		 }
	  });
	  imgCoupon = (ImageView) bottomNavigationView.findViewById(R.id.imgCoupon_navigation);
	  imgCoupon.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			// TODO返回首页
			if(currentIndex==3)
			{
			   return;
			}

			if (Session.getInstance().IsLogin())
			{
			    Intent intent = new Intent();
			    intent.setClass(BaseUINoTop.this, UICouponList.class);
			    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			    startActivity(intent);
			}
			else
			{	
//			   SetNavigationStatus(3);
			   Intent intent = new Intent();
			   intent.setClass(BaseUINoTop.this, UILogin.class);
			   intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			   intent.putExtra(PubConst.Intent_Key.LOGIN_FROM_ACTIVITY,PubConst.ActivityCode.COUPON_LIST);
			   startActivity(intent);
			}

		 }
	  });
	  imgShare = (ImageView) bottomNavigationView.findViewById(R.id.imgShare_navigation);
	  imgShare.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			// TODO返回首页
			if(currentIndex==4)
			{
			   return;
			}

			if (Session.getInstance().IsLogin())
			{

			}
			else
			{	
//			   SetNavigationStatus(4);
//			   Intent intent = new Intent();
//			   intent.setClass(BaseUI.this, UILogin.class);
//			   intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//			   startActivity(intent);
			}
		 }
	  });
	  imgMore = (ImageView) bottomNavigationView.findViewById(R.id.imgMore_navigation);
	  imgMore.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			// TODO更多
			if(currentIndex==5)
			{
			   return;
			}
//			SetNavigationStatus(5);
			Intent intent = new Intent();
			intent.setClass(BaseUINoTop.this, UIbaidu.class);
			startActivity(intent);
		 }
	  });

	  contentView = mInflater.inflate(SetContentViewLayoutResId(), null);
	  contentView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1));
	  mainLayout.addView(contentView);

	  bottomNavigationView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
	  mainLayout.addView(bottomNavigationView);
   }

   /**
    * 设置选中状态
    * 
    * @param selectIndex
    *           1：选中订单 2:选中收藏 3：选中优惠 4：更多
    */
   protected void SetNavigationStatus(int selectIndex)
   {
	  switch (selectIndex)
	  {
		 case 1:
			currentIndex=1;
			imgOrder.setImageResource(R.drawable.navigation_myorder_dw);
			imgFavorites.setImageResource(R.drawable.navigation_favorites_nm);
			imgCoupon.setImageResource(R.drawable.navigation_coupon_nm);
//			imgShare.setImageResource(R.drawable.navigation_share_nm);
			imgMore.setImageResource(R.drawable.navigation_more_nm);
			break;
		 case 2:
			currentIndex=2;
			imgOrder.setImageResource(R.drawable.navigation_myorder_nm);
			imgFavorites.setImageResource(R.drawable.navigation_favorites_dw);
			imgCoupon.setImageResource(R.drawable.navigation_coupon_nm);
//			imgShare.setImageResource(R.drawable.navigation_share_nm);
			imgMore.setImageResource(R.drawable.navigation_more_nm);
			break;
		 case 3:
			currentIndex=3;
			imgOrder.setImageResource(R.drawable.navigation_myorder_nm);
			imgFavorites.setImageResource(R.drawable.navigation_favorites_nm);
			imgCoupon.setImageResource(R.drawable.navigation_coupon_dw);
//			imgShare.setImageResource(R.drawable.navigation_share_nm);
			imgMore.setImageResource(R.drawable.navigation_more_nm);
			break;
//		 case 4:
//			currentIndex=4;
//			imgOrder.setImageResource(R.drawable.navigation_myorder_nm);
//			imgFavorites.setImageResource(R.drawable.navigation_favorites_nm);
//			imgCoupon.setImageResource(R.drawable.navigation_coupon_nm);
////			imgShare.setImageResource(R.drawable.navigation_share_dw);
//			imgMore.setImageResource(R.drawable.navigation_more_nm);
//			break;
		 case 4:
			currentIndex=4;
			imgOrder.setImageResource(R.drawable.navigation_myorder_nm);
			imgFavorites.setImageResource(R.drawable.navigation_favorites_nm);
			imgCoupon.setImageResource(R.drawable.navigation_coupon_nm);
//			imgShare.setImageResource(R.drawable.navigation_share_nm);
			imgMore.setImageResource(R.drawable.navigation_more_dw);
			break;
	  }
   }

   protected void CleanNavigationStatus()
   {
	  imgOrder.setImageResource(R.drawable.navigation_myorder_nm);
	  imgFavorites.setImageResource(R.drawable.navigation_favorites_nm);
	  imgCoupon.setImageResource(R.drawable.navigation_coupon_nm);
//	  imgShare.setImageResource(R.drawable.navigation_share_nm);
	  imgMore.setImageResource(R.drawable.navigation_more_nm);
   }

  

   @Override
   final protected void onCreate(Bundle savedInstanceState)
   {
	  // 设置标题不显示
	  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
	  super.onCreate(savedInstanceState);

	  // 创建出完整的页面Layout，并设置为当前Activity的页面。
	  mainLayout = new LinearLayout(this);
	  LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
	  mainLayout.setOrientation(LinearLayout.VERTICAL);
	  mainLayout.setLayoutParams(layoutParams);
	  setContentView(mainLayout);

	  InitBaseControls();

	  // 回调子类,正常处理onCreate方法。
	  onCreateOverride(savedInstanceState);
   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
	  if (keyCode == event.KEYCODE_BACK)
	  {
		 finish();
	  }
	  return false;
   }

   
   /**
    * 子类实现后，在原来的onCreate方法中内容移到这里来操作。
    * 
    * @param savedInstanceState
    */
   protected abstract void onCreateOverride(Bundle savedInstanceState);

   /**
    * 返回layout xml的ID
    * 原本在Activity的onCreate方法中调用的setContentView(R.layout.xxxxLayoutId); 现在从该方法返回。
    * 该方法在onCreateOverride之前执行
    * 
    * @return
    */
   public abstract int SetContentViewLayoutResId();

   /**
    * 为避免子类错误调用，覆盖该方法，并定义为空方法。
    */
   public void setContentView(int layoutResID)
   {
   }

   /**
    * 执行异步任务
    */
   public String executeAsyncTask()
   {
	  return getTaskManager().executeAsyncTask();
   }

   /**
    * 执行异步任务
    * 
    * @param taskName
    */
   public String executeAsyncTask(String taskName)
   {
	  return getTaskManager().executeAsyncTask(taskName);
   }

   /**
    * 执行异步任务
    * 
    * @param taskName
    */
   public String executeAsyncTask(String taskName, Object[] params)
   {
	  return getTaskManager().executeAsyncTask(taskName, params);
   }

   /**
    * 取消异步任务
    */
   public void cancelAsyncTask()
   {
	  getTaskManager().cancelAsyncTask();
   }

   /**
    * 取消异步任务
    * 
    * @param taskId
    */
   public void cancelAsyncTask(String taskId)
   {
	  getTaskManager().cancelAsyncTask(taskId);
   }

   /**
    * 异步任务开始
    */
   public void onAsyncTaskStart(String taskName, Object[] params)
   {

   }

   /**
    * 异步任务执行
    * 
    * @param taskName
    */
   public Object onAsyncTaskInBackground(String taskName, Object[] params) throws Exception
   {
	  return null;
   }

   /**
    * 异步任务完成
    * 
    * @param arg
    */
   public void onAsyncTaskEnd(String taskId, String taskName, Object[] params, Object result)
   {

   }

   /**
    * 异步任务被取消
    */
   public void onAsyncTaskCancelled(String taskName)
   {

   }

   /**
    * 异步任务发生错误
    * 
    * @param e
    */
   public void onAsyncTaskError(String taskId, String taskName, Exception e)
   {
	  // if (waitDialog != null)
	  // {
	  // waitDialog.dismiss();
	  // waitDialog = null;
	  // }
	  //
	  // if (e != null)
	  // {
	  // String msg = "";
	  // if (e instanceof SocketException)
	  // {
	  // msg = "手机网络不可用或发生网络读写错误";
	  // }
	  // else
	  // {
	  // msg = "手机网络不可用或发生网络读写错误";
	  // }
	  //
	  // if (msg != null && !msg.equals(""))
	  // {
	  // Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	  // }
	  // }
   }

}
